package com.example.android.miwok;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class WordAdapter extends ArrayAdapter<Word> {

    //Resource ID for the background color for this list of word
    private int colorResourceId;

    /**
     * This is our own custom constructor (it doesn't mirror a superclass constructor).
     * The context is used to inflate the layout file, and the list is the data we want
     * to populate into the lists.
     *
     * @param context        The current context. Used to inflate the layout file.
     * @param words      A List of word objects to display in a list
     */
    public WordAdapter(Activity context, ArrayList<Word> words, int colorResourceId){
        super(context, 0, words);
        this.colorResourceId = colorResourceId;
    }

    /**
     * Provides a view for an AdapterView (ListView, GridView, etc.)
     *
     * @param position The position in the list of data that should be displayed in the list item view.
     * @param convertView The recycled view to populate.
     * @param parent The parent ViewGroup that is used for inflation.
     * @return The View for the position in the AdapterView.
     */
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        //return super.getView(position, convertView, parent);

        // Check if the existing view is being reuse,otherwise inflate the view
        // (if convertView is null,there is no view to reuse, In this case we will need to inflate
        // one from the list item layout from scratch)
        View listItemView = convertView;
        if(listItemView == null){
            //LayoutInflater is an abstract class,and have no static method :inflate(),so
            //we should use LayoutInflater.from(getContext()) method to obtain a LayoutInflater
            //from the given context
            //
            //The third param false, because we don't want to attach the list item view to the parent list view.
            //The'getContext()' parameter is defined in ArrayAdapter.
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }
        //Get the object located at this position in the list. getItem() method from the super class ArrayAdapter
        final Word currentWord =  getItem(position);

        //Find the TextView in the list_item.xml layout with the id.
        //ArrayAdapter don't have the method of 'findViewById()',
        //so must have to use the method of 'View.findViewById()' to find the TextView
        TextView defaultTextView =  (TextView)listItemView.findViewById(R.id.default_text_view);
        defaultTextView.setText(currentWord.getDefaultTranslation());

        //Find the TextView in the list_item.xml layout with the id
        TextView miwokTextView = (TextView)listItemView.findViewById(R.id.miwok_text_view);
        miwokTextView.setText(currentWord.getMiwokTranslation());

        if (currentWord.checkImageResource()) {
            //Find the ImageView in the list_item.xml layout with the id
            ImageView imageView = (ImageView)listItemView.findViewById(R.id.image_view);
            imageView.setImageResource(currentWord.getImageResourceId());
        }else{
            ImageView imageView = (ImageView)listItemView.findViewById(R.id.image_view);
            imageView.setVisibility(View.GONE);
        }

        LinearLayout textLinearLayout = (LinearLayout)listItemView.findViewById(R.id.text_linear_layout);
        textLinearLayout.setBackgroundResource(colorResourceId);


//        textLinearLayout.setOnClickListener(new View.OnClickListener(){
//            public void onClick(final View view){
//                MediaPlayer mediaPlayer = MediaPlayer.create(getContext(), currentWord.getAudioResourceId());
//                Toast.makeText(view.getContext(), "Play", Toast.LENGTH_SHORT).show();
//                mediaPlayer.start();
//                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
//
//                    public void onCompletion(MediaPlayer mediaPlayer){
//                        //Toast.makeText(view.getContext(),"I'm done!", Toast.LENGTH_SHORT).show();
//                        mediaPlayer.release();
//                    }
//                });
//            }
//        });


        return listItemView;
    }
}
